<!--
 * @Descripttion: 视频分类
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-11-25 10:57:01
-->
<template>
  <div class="lb-website-video">
    <top-nav :title="navTitle" :isBack="true"></top-nav>
    <div class="page-main">
      <el-row class="page-top-operate">
        <lb-button
          type="primary"
          icon="el-icon-plus"
          @click="addEditClassify('add')"
          >新增分类</lb-button
        >
      </el-row>
      <el-dialog
        :title="dialogForm.id ? '编辑分类' : '新增分类'"
        :visible.sync="centerDialogVisible"
        width="500px"
        center
      >
        <el-form
          @submit.native.prevent
          :model="dialogForm"
          :rules="dialogFormRules"
          ref="dialogForm"
          label-width="100px"
        >
          <el-form-item label="分类名" prop="name">
            <el-input
              v-model="dialogForm.name"
              placeholder="请输入分类名"
              maxlength="8"
              show-word-limit
            ></el-input>
          </el-form-item>
          <el-form-item label="排序值" prop="top">
            <el-input-number
              class="lb-input-number"
              v-model="dialogForm.top"
              :controls="false"
              :precision="0"
              :min="1"
              placeholder="请输入排序值"
            ></el-input-number>
            <lb-tool-tips>值越大, 排序越靠前</lb-tool-tips>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="centerDialogVisible = false">{{
            $t('action.cancel')
          }}</el-button>
          <el-button type="primary" @click="submitFormInfo('dialogForm')">{{
            $t('action.comfirm')
          }}</el-button>
        </span>
      </el-dialog>
      <!-- 搜索 -->
      <!-- <el-row class="page-top-operate form-search">
        <el-form @submit.native.prevent :inline="true" :model="searchForm">
          <el-form-item label="标题">
            <el-input v-model="searchForm.keyword" placeholder="请输入分类标题"></el-input>
          </el-form-item>
          <lb-button type="primary" @click="getTableDataList(1)">{{$t('action.search')}}</lb-button>
        </el-form>
      </el-row> -->
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="name" label="分类名称"></el-table-column>
        <el-table-column prop="video_count" label="内容数量"></el-table-column>
        <el-table-column prop="status" label="是否显示">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="handleSwitchChange(scope.row.status, scope.row.id)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="top" label="分类排序"></el-table-column>
        <el-table-column prop="name" label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                plain
                type="primary"
                @click="
                  addEditClassify('edit', {
                    id: scope.row.id,
                    name: scope.row.name,
                    top: scope.row.top,
                    status: scope.row.status
                  })
                "
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="confirmDelVideo(scope.row.id)"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      ></lb-page>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      navTitle: '',
      modular_id: '',
      searchForm: {
        page: 1,
        limit: 10,
        modular_id: '',
        keyword: ''
      },
      total: 0,
      tableData: [],
      loading: false,
      status: 1,
      centerDialogVisible: false,
      dialogForm: {
        name: '',
        top: 1,
        id: '',
        status: 1,
        modular_id: ''
      },
      dialogFormRules: {
        name: {
          required: true,
          type: 'string',
          message: '请输入分类名',
          trigger: 'blur'
        },
        top: {
          required: true,
          type: 'number',
          message: '请输入排序值',
          trigger: 'blur'
        }
      }
    }
  },
  created () {
    let { id, title } = this.$route.query
    this.searchForm.modular_id = id
    this.dialogForm.modular_id = id
    this.navTitle = title
    this.getTableDataList()
  },
  components: {
  },
  methods: {
    addEditClassify (type, formInfo) {
      let obj = {
        name: '',
        top: 1,
        id: '',
        status: 1
      }
      let result = type === 'add' ? obj : formInfo
      result.modular_id = this.searchForm.modular_id
      this.dialogForm = result
      this.centerDialogVisible = true
    },
    submitFormInfo (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          let { dialogForm, status } = this
          dialogForm.status = status ? 1 : 0
          let methodModel = dialogForm.id
            ? 'websiteVideoUpdateCategory'
            : 'websiteVideoAddCategory'
          this.$api[methodModel](dialogForm).then(res => {
            if (res.code === 200) {
              this.$message.success(
                this.$t(dialogForm.id ? 'tips.successRev' : 'tips.successSave')
              )
              this.centerDialogVisible = false
              this.getTableDataList()
            }
          })
        }
      })
    },
    addEditVideo (type, aid) {
      let { id } = this.searchForm
      let url =
        type === 'add'
          ? `/website/editVideo?wid=${id}`
          : `/website/editVideo?wid=${id}&id=${aid}`
      this.$router.push(url)
    },
    /**
     * @method 获取列表数据
     */
    getTableDataList (page) {
      this.loading = true
      this.searchForm.page = page || this.searchForm.page
      this.$api.websiteVideoCategory(this.searchForm).then(res => {
        console.log(res)
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    handleSwitchChange (status, id) {
      let methodModel = status === 2 ? 'websiteVideoDelCategory' : 'websiteVideoStatusCategory'
      let param = status === 2 ? { id } : { id, status }
      this.$api[methodModel](param)
        .then(res => {
          if (res.code === 200) {
            this.$message.success(
              status === 2
                ? this.$t('tips.successDel')
                : this.$t('tips.successRev')
            )
            if (status === 2) {
              this.searchForm.page =
                this.searchForm.page <
                  Math.ceil((this.total - 1) / this.searchForm.limit)
                  ? this.searchForm.page
                  : Math.ceil((this.total - 1) / this.searchForm.limit)
              this.getTableDataList()
            }
          } else {
            if (status === 2) return
            this.getTableDataList()
          }
        })
    },
    /**
     * @method 删除数据
     */
    confirmDelVideo (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.handleSwitchChange(2, id)
      })
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-website-video {
  width: 100%;
  .page-main {
    width: 100%;
    .el-input,
    .el-input-number {
      width: 300px;
    }
  }
}
</style>
